package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.core.exception.CustomException;
import com.dyna.core.utils.ObjectUtil;
import com.dyna.core.utils.PageUtil;
import com.dyna.domain.req.InsertValidateGroup;
import com.dyna.domain.req.PageParam;
import com.dyna.domain.req.UpdateValidateGroup;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.excel.core.util.ExcelUtil;
import com.dyna.foundation.domain.entity.SysDictData;
import com.dyna.foundation.domain.entity.SysDictType;
import com.dyna.foundation.domain.form.SysDictTypeSaveForm;
import com.dyna.foundation.domain.req.SysDictDataReq;
import com.dyna.foundation.domain.req.SysDictTypePageReq;
import com.dyna.foundation.domain.req.SysDictTypeReq;
import com.dyna.foundation.domain.resp.SysDictTypeResp;
import com.dyna.foundation.domain.resp.SysDictTypeSimpleResp;
import com.dyna.foundation.feign.FoundationDictApi;
import com.dyna.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zhoucaiwang
 * @date 2023-08-05
 */
@Api(value = "字典类型")
@RestController
@RequestMapping(value = "/sys-dict")
public class SysDictTypeController implements FoundationDictApi {

    private final SysDictTypeService sysDictTypeService;

    @Autowired
    public SysDictTypeController(SysDictTypeService sysDictTypeService) {
        this.sysDictTypeService = sysDictTypeService;
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建字典类型")
    public R<Long> createDictType(@Valid @RequestBody SysDictTypeSaveForm createReqVO) {
        Long dictTypeId = sysDictTypeService.createDictType(createReqVO);
        return R.data(dictTypeId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改字典类型")
    public R<Boolean> updateDictType(@Valid @RequestBody SysDictTypeSaveForm updateReqVO) {
        sysDictTypeService.updateDictType(updateReqVO);
        return R.data(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<Boolean> deleteDictType(Long id) {
        sysDictTypeService.deleteDictType(id);
        return R.data(true);
    }

    @GetMapping("/page")
    @ApiOperation(value = "获得字典类型的分页列表")
    public R<PageResult<SysDictTypeResp>> pageDictTypes(@Valid SysDictTypePageReq pageReqVO) {
        PageResult<SysDictTypeResp> pageResult = sysDictTypeService.getDictTypePage(pageReqVO);
        return R.data(pageResult);
    }

    @ApiOperation(value = "/查询字典类型详细")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    @GetMapping(value = "/get")
    public R<SysDictTypeResp> getDictType(@RequestParam("id") Long id) {
        SysDictType dictType = sysDictTypeService.getDictType(id);
        return R.data(BeanUtil.toBean(dictType, SysDictTypeResp.class));
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @ApiOperation(value = "获得全部字典类型列表", notes = "包括开启 + 禁用的字典类型，主要用于前端的下拉选项")
    // 无需添加权限认证，因为前端全局都需要
    public R<List<SysDictTypeSimpleResp>> getSimpleDictTypeList() {
        List<SysDictType> list = sysDictTypeService.getDictTypeList();
        return R.data(PageUtil.convertPageResult(list, SysDictTypeSimpleResp.class));
    }

    @ApiOperation(value = "导出数据类型")
    @GetMapping("/export")
    public void export(HttpServletResponse response, @Valid SysDictTypePageReq exportReqVO) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysDictTypeResp> list = sysDictTypeService.getDictTypePage(exportReqVO).getList();
        // 导出
        ExcelUtil.write(response, "字典类型.xls", "数据", SysDictTypeResp.class, list);
    }

    /*************************************************************************************************/

    @ApiOperation(value = "分页查询字典类型")
    @PostMapping("/getDictTypePage")
    public R<PageResult<SysDictTypeResp>> getDictTypePage(@RequestBody SysDictTypeReq req) {
        return R.data(sysDictTypeService.getDictTypePage(req));
    }

    @ApiOperation(value = "添加字典数据")
    @RequestMapping("/addSysDictType")
    public R<Integer> addSysDictType(@RequestBody @Validated(value = {InsertValidateGroup.class}) SysDictType sysDictType) {
        return R.isSuccess(sysDictTypeService.addSysDictType(sysDictType));
    }

    @ApiOperation(value = "修改字典数据")
    @RequestMapping("/editSysDictType")
    public R<Boolean> editSysDictType(@RequestBody @Validated(value = {UpdateValidateGroup.class}) SysDictType sysDictType) {
        return R.isSuccess(sysDictTypeService.editSysDictType(sysDictType));
    }

    @ApiOperation(value = "删除字典类型")
    @RequestMapping("/delSysDictType")
    public R<Boolean> delSysDictType(@RequestParam(value = "dictId",required = true) Integer dictId) {
        if (ObjectUtil.isNull(dictId)) {
            throw new CustomException("字典id不能为空");
        }
        return R.isSuccess(sysDictTypeService.delSysDictType(dictId));
    }

    @ApiOperation(value = "修改字典类型状态")
    @GetMapping("/changeSysDictTypeStatus")
    public R<Boolean> changeSysDictTypeStatus(@RequestParam("dictId") Integer dictId,
                                   @RequestParam("status") Integer status
                                   ) {
        return R.isSuccess(sysDictTypeService.changeSysDictTypeStatus(dictId,status));
    }

    @ApiOperation(value = "分页查询字典数据列表")
    @PostMapping("/getSysDictDataPage")
    public R<PageResult<SysDictData>> getSysDictDataPage(@RequestBody SysDictDataReq req) {
        return R.data(sysDictTypeService.getSysDictDataPage(req));
    }

    @ApiOperation(value = "查询字典数据列表")
    @PostMapping("/getSysDictDataList")
    public R<List<SysDictData>> getSysDictDataList(@RequestBody SysDictDataReq req) {
        return R.data(sysDictTypeService.getSysDictDataList(req));
    }

    @ApiOperation(value = "新增字典数据")
    @PostMapping("/addSysDictData")
    public R<Boolean> addSysDictData(@RequestBody @Validated(value = {InsertValidateGroup.class}) SysDictData sysDictData) {
        return R.isSuccess(sysDictTypeService.addSysDictData(sysDictData));
    }

    @ApiOperation(value = "编辑字典数据")
    @PostMapping("/editSysDictData")
    public R<Boolean> editSysDictData(@RequestBody @Validated(value = {UpdateValidateGroup.class}) SysDictData sysDictData) {
        return R.isSuccess(sysDictTypeService.editSysDictData(sysDictData));
    }

    @ApiOperation(value = "删除字典数据")
    @GetMapping("/delSysDictData")
    public R<Boolean> delSysDictData(@RequestParam(value = "dictId") Integer dictId) {
        if (ObjectUtil.isNull(dictId)) {
            throw new CustomException("字典数据id不能为空");
        }
        return R.isSuccess(sysDictTypeService.delSysDictData(dictId));
    }

    @ApiOperation(value = "修改字典数据状态")
    @GetMapping("/changeSysDictDataStatus")
    public R<Boolean> changeSysDictDataStatus(@RequestParam("dictId") Integer dictId,
                                              @RequestParam("status") Integer status
    ) {
        return R.isSuccess(sysDictTypeService.changeSysDictDataStatus(dictId,status));
    }

    @Override
    @ApiOperation(value = "批量查询字典数据")
    @PostMapping("/getBatchSysDictList")
    public R<Map<String, List<SysDictData>>> getBatchSysDictList(List<String> dictTypeList) {
        return R.data(sysDictTypeService.getBatchSysDictList(dictTypeList));
    }

    @Override
    @ApiOperation(value = "根据字典类型查询字典Map数据")
    @GetMapping("/getSysDictDataMapByType")
    public R<Map<Integer, String>> getSysDictDataMapByType(@RequestParam(value = "dictType",required = true) String dictType) {
        Map<Integer, String> map = sysDictTypeService.getSysDictDataMapByType(dictType);
        return R.data(map);
    }

    @ApiOperation(value = "根据字典类型查询字典数据")
    @GetMapping("/getSysDictDataByType")
    public R<List<SysDictData>> getSysDictDataByType(@RequestParam(value = "dictType",required = true) String dictType) {
        return R.data(sysDictTypeService.getSysDictDataByType(dictType));
    }

}